package webjoch.process.strategies.bosses;

import com.rsbuddy.script.methods.Npcs;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.util.Filter;
import com.rsbuddy.script.wrappers.Npc;

import webjoch.misc.Functions;
import webjoch.process.strategies.Boss;

public class AstreaFrostweb extends Boss{
    //Protecting from melee = 9980, mage = 9981, range = 9982,
    
    public final int[] bossID = {9980, 9981, 9982};
    public final int[] ice_spiders = {10157, 10158};
    public final int[] recognice = {51148};
    
    
    @Override
    public boolean isValid() {
        return room.getObject(recognice) != null;
    }

    @Override
    public boolean isFinished() {
        return room.getObject(STRANGE_WALL_OPEN) != null;
    }

    @Override
    public BossType getBossType() {
        return BossType.ASTEA_FROSTWEB;
    }

    @Override
    public int loop() {
        Npc boss = Npcs.getNearest(bossID);
        Npc[] spiders = getSpiders();
        Npc closestSpider = Functions.getNearest(spiders);
        com.rsbuddy.script.wrappers.Character interact = Players.getLocal().getInteracting();
        
        if (boss == null) {
            if (isFinished()) {
                System.out.println("Boss " + getBossType());
                return 0;
            }
        }
        if (interact == null || !interact.equals(boss)) {
            if (isProtecting()) {
                if (getTotalSpiders() > 0) {
                    if (closestSpider != null) {
                        closestSpider.interact("Attack");
                    }
                    return 500;
                } else {
                    return 50;
                }
            } else {
                boss.interact("Attack");
                return 500;
            }
        } else { //IS attacking
            if (isProtecting()) {
                if (getTotalSpiders() > 0) {
                    if (Functions.inArray(spiders, Players.getLocal().getInteracting())) {
                        return 50;
                    } else {
                        if (closestSpider != null) {
                            closestSpider.interact("Attack");
                        }
                        return 500;
                    }
                } else {
                   // boss.interact("Attack"); //already attacking boss
                    return 500;
                }
            } else {
                //boss.interact("Attack"); //already attacking boss
                return 500;
            }
        }
    }
    
    
    public int getBossStyle() {
        Npc boss = Npcs.getNearest(bossID);
        if (boss != null) return boss.getId();
        return -1;
    }
    
    public boolean isProtecting() {
        //TODO Get an method defining the attack style (mage, range, melee)
        //Currently it takes like you ALWAYS fight with melee
        return (getBossStyle() == 9980);
    }
    
    public boolean isFrozen() {
        //TODO check if settings change when frozen @ this boss.
        return false;
    }
    
    public Npc[] getSpiders() {
        return Npcs.getLoaded(new Filter<Npc>() {
           public boolean accept(Npc npc) {
               if (npc == null) return false;
               return (room.getArea().contains(npc) && Functions.inArray(ice_spiders, npc.getId()));
           }
        });
    }
    public int getTotalSpiders() {
        return getSpiders().length;
        
        
    }
    
  //astrea frostweb, 9980 = melee, 9981 = mage, 9982 = ranged
  //ice spider: 10157
}
